<?php

class Obj_DBTable_Cities extends Zend_Db_Table_Abstract
{
    protected $_name = 'cities';
    protected $_primary = 'ID';

    
    public function getCities($region, $activeOnly = false) {
    	$select = $this->select()->from($this)->order('Name')->where('RegionID = ?', (int) $region);
    	if ($activeOnly === true) $select->where('active = 1');
    	return $this->fetchAll($select)->toArray();
    }
    
    public function getAllCities($activeOnly = false) {
    	$select = $this->select()->from($this)->order('Name');
    	if ($activeOnly === true) $select->where('active = 1');
    	return $this->fetchAll($select)->toArray();
    }
     
    public function getCountryCities($country, $activeOnly = false) {
    	$select = $this->select()->from($this)->where('cities.CountryID = ?', (int) $country);
    	$select->join('regions', 'regions.ID = cities.RegionID', array('RName' => 'Name'));
    	$select->order('regions.Name')->order('cities.Name');
    	if ($activeOnly === true) $select->where('cities.active = 1');
    	$select->setIntegrityCheck(false);
    	return $this->fetchAll($select)->toArray();
    }
     
    public function getCity($id) {
    	$select = $this->select()->from($this)->where('ID = ?', (int) $id);
    	$res = $this->fetchRow($select);
    	if ($res) {
    		return $res->toArray();
    	}
    	return false;
    }
     
    public function findCity($alias) {
    	$select = $this->select()->from($this)->where('alias = ?', $alias)->where('active = 1');
    	$res = $this->fetchRow($select);
    	if ($res) {
    		return $res->toArray();
    	}
    	return false;
    }
     
    public function getLinkRow($id) {
    	$select = $this->select()->from($this, array('a2' => 'alias'));
    	$select->join('regions', 'regions.ID = cities.RegionID', array('a1' => 'alias'));
    	$select->join('countries', 'cities.CountryID = countries.ID', array('a0' => 'alias'));
    	$select->where('cities.ID = ?', $id);
    	$select->setIntegrityCheck(false);
    	$res = $this->fetchRow($select);
    	if ($res) {
    		return $res->toArray();
    	}
    	return false;
    }
}

